package cn.com.gridinfo.sooc.core.course.classes.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;

import com.jeremy.arad.utils.AnimUtil;
import com.jeremy.arad.utils.Log;
import com.marshalchen.ultimaterecyclerview.UltimateRecyclerView;

import org.apache.http.message.BasicNameValuePair;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.base.BaseSoocActivity;
import cn.com.gridinfo.sooc.core.course.classes.adapter.CourseListAdapter;
import cn.com.gridinfo.sooc.core.course.classes.dao.CourseDao;
import cn.com.gridinfo.sooc.core.course.common.CodeConst;
import cn.com.gridinfo.sooc.utils.IntentUtil;
import cn.com.gridinfo.sooc.utils.MessageUtils;

/**
 * 课程列表界面
 *
 * @author  jie.li
 */
public class CourseListActivity extends BaseSoocActivity {


    @InjectView(R.id.course_main_list)
    UltimateRecyclerView course_main_recylerView;//课程列表

    CourseListAdapter courseListAdapter;//课程列表适配器

    CourseDao dao;//课程分类 dao
    LinearLayoutManager linearLayoutManager;//列表管理器

    String yxid;//课程分类ID
    String yxmc;//院系名称
    int pageNo = 1;
    int pageSize = 10;
    int maxLastPositon;//数据最后位置
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.course_main_list);
        ButterKnife.inject(this);
        Intent i = getIntent();
        yxid = i.getStringExtra("yxid");//课程分类ID
        yxmc = i.getStringExtra("yxmc");//院系名称
        dao = new CourseDao(this);
        //获取课程列表
        dao.getCourseListByPage(yxid,"",pageNo, CodeConst.COURSE_LIST_FIRST);;

        //初始化listView
        linearLayoutManager = new LinearLayoutManager(this);
        course_main_recylerView.setLayoutManager(linearLayoutManager);

    }

    //设置标题
    @Override
    public String setupToolBarTitle() {
        return yxmc;
    }

    @Override
    public boolean setupToolBarLeftButton(ImageView leftButton) {
        return true;
    }

    //点击返回
    @OnClick(R.id.toolbar_leftbtn)
    void goBack(){
        finish();
        AnimUtil.intentSlidOut(this);
    }


    /**
     * 请求成功
     */
    @Override
    public void onRequestSuccess(int requestCode) {
        try {
            if(dao.status.equals("0")) {
                switch (requestCode) {
                    //课程首次请求
                    case CodeConst.COURSE_LIST_FIRST:
                        initCourseList();
                        break;

                    //课程刷新操作
                    case CodeConst.COURSE_LIST_PUSHDOWN:
                        //刷新结束
                        course_main_recylerView.setRefreshing(false);

                        if (dao.getCourseList() != null && !dao.getCourseList().isEmpty()) {
                            course_main_recylerView.hideEmptyView();
                        } else {
                            //无数据处理
                            course_main_recylerView.showEmptyView();
                        }
                        //刷新数据
                        courseListAdapter.refresh(dao.getCourseList());


                        if (dao.getCourseList().size() > (pageSize - 1))
                            //数据大于每页显示的条数,则加载更多数据 view 显示
                            courseListAdapter.loadMoreDataView();
                        else {
                            //数据小于当前页的话则不显示加载更多,显示没有更多了
                            courseListAdapter.loadNoDataView();
                            linearLayoutManager.scrollToPositionWithOffset(maxLastPositon, -1);
                        }
                        break;

                    //课程翻页操作
                    case CodeConst.COURSE_LIST_PULLUP:
                        if (dao.getCourseList() != null && !dao.getCourseList().isEmpty()) {
                            //加载更多
                            courseListAdapter.loadmore(dao.getCourseList());
                        } else {
                            //数据加载完成,没有更多数据,显示没有更多了
                            courseListAdapter.loadNoDataView();
                            //无数据则不显示下一页
                            linearLayoutManager.scrollToPositionWithOffset(maxLastPositon, -1);
                        }
                        break;

                }
            }else{
                MessageUtils.showImageShortToast(this,dao.statusCode);
            }

        }catch (Exception e){
            MessageUtils.showImageShortToast(this,"请求数据异常");
        }
    }



    /**
     * 初始化 list 数据 分页
     */
    private void  initCourseList(){

        courseListAdapter = new CourseListAdapter(dao.getCourseList(),this);
        course_main_recylerView.setAdapter(courseListAdapter);

        course_main_recylerView.enableLoadmore();
        course_main_recylerView.setDefaultOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener(){
            @Override
            public void onRefresh() {
                        //下拉刷新 账户明细数据
                        pageNo = 1;//首页
                        dao.getCourseListByPage(yxid, "",pageNo, CodeConst.COURSE_LIST_PUSHDOWN);
                        linearLayoutManager.scrollToPosition(0);
                    }
        });
        course_main_recylerView.setOnLoadMoreListener(new UltimateRecyclerView.OnLoadMoreListener() {
            @Override
            public void loadMore(final int itemsCount, final int maxLastVisiblePosition) {
                        if (itemsCount >( pageSize-1)) {
                            maxLastPositon = maxLastVisiblePosition;
                            dao.getCourseListByPage(yxid, "",++pageNo, CodeConst.COURSE_LIST_PULLUP);
                        } else {
                            //加载数据完成,没有更多数据,显示没有更多了
                            courseListAdapter.loadNoDataView();
                            linearLayoutManager.scrollToPositionWithOffset(maxLastVisiblePosition, -1);
                        }
                    }
        });


        courseListAdapter.setmOnItemClickLitener(new CourseListAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(View view, int position) {
                Log.d("点击第"+position+"个");
                String kcid = courseListAdapter.courseList.get(position).getKcid();
                String kcfm = courseListAdapter.courseList.get(position).getKcfm();
                Log.d("课程ID:"+kcid);
                IntentUtil.intentSlidIn(CourseListActivity.this,CourseDetailActivity.class,00,new BasicNameValuePair("kcid",kcid),new BasicNameValuePair("kcfm",kcfm));
            }
        });
    }

}
